package com.lifeonwalden.ms.web.controller;

import com.lifeonwalden.ms.auth.controller.BaseLoginController;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class LoginController extends BaseLoginController {

    @Value("${system.auth.url.success}")
    private String authUrlSuccess;

    @Value("${system.auth.url.login}")
    private String authUrlLogin;

    @GetMapping("/open/login")
    public void login(@RequestParam("userId") String userId, HttpServletRequest request, HttpServletResponse response) {
        String redirectUrl = authUrlSuccess;
        try {
            doLogin(userId, request);
        } catch (AuthenticationException e) {
            redirectUrl = authUrlLogin;
        }

        try {
            response.sendRedirect(redirectUrl);
        } catch (IOException e) {
            throw new RuntimeException("Redirect Error");
        }

    }
}
